#!/bin/bash
if [ ! -f "/etc/uuid" ]; then
  /usr/bin/uuidgen > /etc/uuid
fi

UUID=`cat /etc/uuid`
CARD=`aplay -l | grep Hi[fF]iBerry`
DIGI=`echo $CARD | grep -i digi`
if [ "$DIGI" != "" ]; then
 CARD="Digi+"
else
 CARD="DAC+"
fi
DRIVER=`aplay -l | grep "\[snd_rpi" | awk -F\[ '{print $2}' | awk -F\] '{print $1}'`
VERSION=`cat /etc/hifiberry.version`

DIGIFIXED=0
DIGIDOP=0
LINEARVOLUME=0

echo Configuring RAAT server for UUID $UUID on $CARD \($DRIVER\)

# Generate correct mixer controls:
VOLCTL='"volume": { "type": "software" }'
MAXFS=192000
DSD=""
SIGNALTYPE="analog"

MIXER=`cat /etc/hifiberry.state | grep CURRENT_MIXER_CONTROL | awk -F= '{print $2}'`

V1='"volume": { "type": "alsa", "optional": "false", "device":"default", "index":0, "name":'
if [ "$CARD" == "DAC+" ]; then
 if [ "$LINEARVOLUME" == "1" ]; then
  V2=$MIXER
  V3=', "mode":"number" },'
 else
  V2=$MIXER
  V3=', "mode":"db","db_step":0.5 ,"db_max":10, "db_min":-100},'
 fi
 VOLCTL=$V1$V2$V3
 MAXFS=192000
elif [ "$CARD" == "Digi+" ]; then
 if [ "$DIGIFIXED" == "1" ]; then
   VOLCTL=""
 else
   VOLCTL='"volume": { "type": "alsa", "optional": "false", "device":"default", "index":0, "name":"Softvol" },'
 fi
 # DOP not supported on HiFiBerryOS
 DSD=""

 MAXFS=192000
 SIGNALTYPE="digital"
fi

# Allow user to set softvol in RAAT. This is incompatible with volume control for other services!
if [ -f "/etc/hifiberry_raat.softvol" ]; then
 VOLCTL='"volume": { "type": "software" },'
fi

PLAYBACKDEVICE="default"


cat <<END >/etc/hifiberry_raat.conf
{
    "vendor":      "HiFiBerry",
    "model":       "$CARD",
    "unique_id":   "$UUID",
    "output":      { "type": "alsa", 
                     "device": "$PLAYBACKDEVICE",
                     "max_pcm_rate": $MAXFS,
                     $DSD
                     "signal_path": [ { "quality": "lossless", "type": "output", "method": "$SIGNALTYPE" } ],
		     "supported_formats": [ 
                         {"sample_type": "pcm", "sample_rate": 44100, "bits_per_sample": 16, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 44100, "bits_per_sample": 24, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 44100, "bits_per_sample": 32, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 88200, "bits_per_sample": 16, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 88200, "bits_per_sample": 24, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 88200, "bits_per_sample": 32, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 176400, "bits_per_sample": 16, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 176400, "bits_per_sample": 24, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 176400, "bits_per_sample": 32, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 48000, "bits_per_sample": 16, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 48000, "bits_per_sample": 24, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 48000, "bits_per_sample": 32, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 96000, "bits_per_sample": 16, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 96000, "bits_per_sample": 24, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 96000, "bits_per_sample": 32, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 192000, "bits_per_sample": 16, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 192000, "bits_per_sample": 24, "channels": 2},
                         {"sample_type": "pcm", "sample_rate": 192000, "bits_per_sample": 32, "channels": 2}
                     ]
                   },
$VOLCTL
    "version":     "$VERSION",
    "transport":   { "type": "hifiberry" }
}
END

